package com.lysj.print.print.job;

import com.lysj.print.print.domain.Printer;
import com.lysj.print.print.domain.PrinterRotation;
import com.lysj.print.print.service.PrinterRotationService;
import com.lysj.print.print.service.PrinterService;
import com.lysj.print.print.util.FeiePrinter;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;

/**
 * @ Author ：drj.
 * @ Date  ：Created in 15:06 2019/3/22
 * @ Description: 飞鹅打印机轮询打印
 **/
@Component
public class FeiePrinterJob {

    @Resource
    private PrinterRotationService printerRotationService;


    @Resource
    private PrinterService printerService;

    @Scheduled(initialDelay = 10000, fixedDelay = 60000)
    public void queryFeiePrinterRotation() {
        // 获取类型为飞鹅打印机且未打印的数据列表
        List<PrinterRotation> printerRotations = printerRotationService.getRepository().findByPrinterIdAndStatusAndDataType("%", PrinterRotation.Status.UNPRINT.getCode(), PrinterRotation.DataType.FEIE.getCode());
        for (PrinterRotation printerRotation : printerRotations) {
            //查询飞鹅打印机获取UKEY,打印机编号
            Printer printer = printerService.findOne(printerRotation.getPrinterId());
            // 调用飞鹅打印
            FeiePrinter.print(printerRotation.getPrintInfo(),printer.getPrintKey(),printer.getPrintSnCode());
            //修改打印数据状态
            printerRotation.setStatus(PrinterRotation.Status.SUCCESSPRINT.getCode());
        }
        printerRotationService.getRepository().save(printerRotations);
    }

}
